package com.cnvai.cms.web.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cnvai.cms.business.api.ArticlesService;
import com.cnvai.cms.business.api.ChannelService;
import com.cnvai.cms.javabean.model.CmsChannel;
import com.cnvai.cms.javabean.vo.NewsBean;
import com.cnvai.cms.web.vo.Datagrid;

@Controller
@RequestMapping("/news")
public class NewsController {

	@Inject
	private ArticlesService articlesService;

	@Inject
	private ChannelService channelService;

	public ArticlesService getArticlesService() {
		return articlesService;
	}

	public void setArticlesService(ArticlesService articlesService) {
		this.articlesService = articlesService;
	}

	public ChannelService getChannelService() {
		return channelService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	@RequestMapping("/popularity")
	@ResponseBody
	public String popularity(BigDecimal arcId) {
		getArticlesService().increasingPopularity(arcId);
		return "success";
	}

	@RequestMapping("/newsList")
	public ModelAndView newsList(BigDecimal arsId, int pageNumber, int pageSize, String keywords) {
		ModelAndView view = new ModelAndView("/news/newsList");
		int total = getArticlesService().totalArticleList(arsId, null);
		List<NewsBean> list = getArticlesService().pageArticleList(pageNumber, pageSize, arsId, null);
		view.addObject("total", total);
		view.addObject("newList", list);
		return view;
	}

	@RequestMapping("/newsContent")
	public ModelAndView newsContent(BigDecimal arcId) {
		getArticlesService().increasingPopularity(arcId);
		ModelAndView view = new ModelAndView("/news/newsView");
		NewsBean bean = getArticlesService().getNewsBean(arcId);
		view.addObject("newbean", bean);
		return view;
	}

	@RequestMapping("/channelList")
	@ResponseBody
	public Datagrid getChannelList() {
		Datagrid data = new Datagrid();
		List<CmsChannel> list = getChannelService().getChannelListALL();
		data.setTotal(list.size());
		data.setRows(list);
		return data;
	}
}
